package com.strange.mdl.module.mdl.service.sutras;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.strange.mdl.framework.common.pojo.PageResult;
import com.strange.mdl.framework.common.util.object.BeanUtils;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.chapters.MdlSutraChaptersSaveReqVO;
import com.strange.mdl.module.mdl.controller.admin.sutras.vo.chapters.MdlSutraChaptersPageReqVO;
import com.strange.mdl.module.mdl.controller.app.sutras.vo.AppMdlSutrasChapterRespVO;
import com.strange.mdl.module.mdl.dal.dataobject.sutras.MdlSutraChaptersDO;
import com.strange.mdl.module.mdl.dal.mysql.sutras.MdlSutraChaptersMapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.List;

import static com.strange.mdl.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.strange.mdl.module.mdl.enums.ErrorCodeConstants.SUTRA_CHAPTERS_NOT_EXISTS;

/**
 * 经书章节 Service 实现类
 *
 * @author 密谛灵总控
 */
@Service
@Validated
public class MdlSutraChaptersServiceImpl implements MdlSutraChaptersService {

    @Resource
    private MdlSutraChaptersMapper sutraChaptersMapper;

    @Override
    public Long createSutraChapters(MdlSutraChaptersSaveReqVO createReqVO) {
        // 插入
        MdlSutraChaptersDO sutraChapters = BeanUtils.toBean(createReqVO, MdlSutraChaptersDO.class);
        sutraChaptersMapper.insert(sutraChapters);
        // 返回
        return sutraChapters.getId();
    }

    @Override
    public void updateSutraChapters(MdlSutraChaptersSaveReqVO updateReqVO) {
        // 校验存在
        validateSutraChaptersExists(updateReqVO.getId());
        // 更新
        MdlSutraChaptersDO updateObj = BeanUtils.toBean(updateReqVO, MdlSutraChaptersDO.class);
        sutraChaptersMapper.updateById(updateObj);
    }

    @Override
    public void deleteSutraChapters(Long id) {
        // 校验存在
        validateSutraChaptersExists(id);
        // 删除
        sutraChaptersMapper.deleteById(id);
    }

    @Override
    public void deleteSutraChaptersListByIds(List<Long> ids) {
        // 校验存在
        validateSutraChaptersExists(ids);
        // 删除
        sutraChaptersMapper.deleteByIds(ids);
    }

    private void validateSutraChaptersExists(List<Long> ids) {
        List<MdlSutraChaptersDO> list = sutraChaptersMapper.selectByIds(ids);
        if (CollUtil.isEmpty(list) || list.size() != ids.size()) {
            throw exception(SUTRA_CHAPTERS_NOT_EXISTS);
        }
    }

    private void validateSutraChaptersExists(Long id) {
        if (sutraChaptersMapper.selectById(id) == null) {
            throw exception(SUTRA_CHAPTERS_NOT_EXISTS);
        }
    }

    @Override
    public MdlSutraChaptersDO getSutraChapters(Long id) {
        return sutraChaptersMapper.selectById(id);
    }

    @Override
    public PageResult<MdlSutraChaptersDO> getSutraChaptersPage(MdlSutraChaptersPageReqVO pageReqVO) {
        return sutraChaptersMapper.selectPage(pageReqVO);
    }

    @Override
    public List<MdlSutraChaptersDO> getSutrasChapterList(Long sutraId) {
        QueryWrapper<MdlSutraChaptersDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(MdlSutraChaptersDO::getId, MdlSutraChaptersDO::getSutraId, MdlSutraChaptersDO::getChapterTitle);
        queryWrapper.lambda().eq(MdlSutraChaptersDO::getSutraId, sutraId);
        List<MdlSutraChaptersDO> sutraChaptersDOList = sutraChaptersMapper.selectList(queryWrapper);
        return sutraChaptersDOList;
    }

    @Override
    public MdlSutraChaptersDO getSutrasChapterContent(Long sutraId, Long chapterId) {
        QueryWrapper<MdlSutraChaptersDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(MdlSutraChaptersDO::getId, MdlSutraChaptersDO::getSutraId,
                MdlSutraChaptersDO::getChapterTitle, MdlSutraChaptersDO::getContent, MdlSutraChaptersDO::getChapterSubtitle);
        queryWrapper.lambda().eq(MdlSutraChaptersDO::getSutraId, sutraId);
        queryWrapper.lambda().eq(MdlSutraChaptersDO::getId, chapterId);
        MdlSutraChaptersDO sutraChaptersDO = sutraChaptersMapper.selectOne(queryWrapper);
        return sutraChaptersDO;
    }

}
